<template>
  <div class="app-container">
    <div class="head">
      <img src="@/assets/img/Layout/position.png" />
      <p class="see-block">采购入库详情</p>
    </div>
    <div class="view-container">
      <el-row class="status-title">
        <template v-if="detailData.orderType == '1'">
          <el-col class="mr20 width100">
            <template v-if="detailData.status == '0' && (detailData.approvalStatus == '1' || detailData.approvalStatus == '0')">
              <el-button
                class="btn-success width100"
                type="primary"
                @click="confirm()"
                v-hasPermi="['system:purInboundOrder:confirm']"
              >确认</el-button>
            </template>
          </el-col>
        </template>
        <template v-else>
          <template v-if="detailData.miningBySales == '0'">
            <el-col class="mr20 width100" v-if="purInboundNoCheck.ifAllow == '1' && detailData.status == '0' || detailData.status == '3'">
              <el-button
                class="btn-default width100"
                type="primary"
                @click="submit()"
                v-hasPermi="['system:purInboundOrder:confirm']"
              >提交</el-button>
            </el-col>
            <template v-if="purInboundNoCheck.ifAllow == '1'">
              <template v-if="detailData.approvalStatus == '0' || detailData.approvalStatus == '3'">
                <el-col class="mr20 width100" v-if="detailData.status !== '3' && detailData.status !== '0'">
                  <el-button
                    class="btn-audit width100"
                    type="primary"
                    @click="audit()"
                    v-hasPermi="['system:purInboundOrder:check']"
                  >审核</el-button>
                </el-col>
              </template>
            </template>
            <el-col class="mr20 width100" v-if="purInboundNoCheck.ifAllow == '0'">
              <template v-if="detailData.status == '0' && (detailData.approvalStatus == '1' || detailData.approvalStatus == '0')">
                <el-button
                  class="btn-success width100"
                  type="primary"
                  @click="confirm()"
                  v-hasPermi="['system:purInboundOrder:confirm']"
                >确认</el-button>
              </template>
            </el-col>
          </template>
          <template v-else>
            <template v-if="detailData.dropShipping == '0'">
              <el-col class="mr20 width100" v-if="purInboundNoCheck.ifAllow == '1' && detailData.status == '0' || detailData.status == '3'">
                <el-button
                  class="btn-default width100"
                  type="primary"
                  @click="submit()"
                  v-hasPermi="['system:purInboundOrder:confirm']"
                >提交</el-button>
              </el-col>
              <template v-if="purInboundNoCheck.ifAllow == '1'">
                <template v-if="detailData.approvalStatus == '0' || detailData.approvalStatus == '3'">
                  <el-col class="mr20 width100" v-if="detailData.status !== '3' && detailData.status !== '0'">
                    <el-button
                      class="btn-audit width100"
                      type="primary"
                      @click="audit()"
                      v-hasPermi="['system:purInboundOrder:check']"
                    >审核</el-button>
                  </el-col>
                </template>
              </template>
              <el-col class="mr20 width100" v-if="purInboundNoCheck.ifAllow == '0'">
                <template v-if="detailData.status == '0' && (detailData.approvalStatus == '1' || detailData.approvalStatus == '0')">
                  <el-button
                    class="btn-success width100"
                    type="primary"
                    @click="confirm()"
                    v-hasPermi="['system:purInboundOrder:confirm']"
                  >确认</el-button>
                </template>
              </el-col>
            </template>
          </template>
        </template>
        <el-col class="mr20 width100" v-if="detailData.approvalStatus == '1'">
          <el-button
            class="btn-scanCodeDel"
            type="primary"
            icon="el-icon-download"
            @click="btnPrint()"
          >打印</el-button>
        </el-col>
        <el-col :span="10" class="status-container">
          <div class="status-item">
            <span :class="{ 'active' : detailData.status == '0' }">待入库</span>
            <span :class="{ 'active' : detailData.status == '1' || detailData.status == '4' }">已入库</span>
          </div>
        </el-col>
      </el-row>
      <el-row class="detail-item">
        <el-col :span="8">
          <span>入库单号：</span>{{ detailData.code ?  detailData.code : '暂无'}}
        </el-col>
        <el-col :span="8">
          <span>关联采购单号：</span>{{ detailData.purchaseOrderCode ?  detailData.purchaseOrderCode : '暂无'}}
        </el-col>
        <el-col :span="8" v-if="closeScan == true">
          <ul class="scan">
              <li @click="onCloseScanCode" class="close">删除扫码</li>
          </ul>
        </el-col>
      </el-row>
      <el-row class="detail-item">
        <el-col :span="8">
          <span>入库日期：</span>{{ detailData.warehouseTime ?  detailData.warehouseTime : '暂无'}}
        </el-col>
        <el-col :span="8">
          <span>供应商：</span>{{ detailData.supplierName ?  detailData.supplierName : '暂无'}}
        </el-col>
        <!-- <el-col :span="8" v-if="detailData.status == '1'">
          <span>仓库：</span>{{ detailData.warehouse ?  detailData.warehouse : '暂无'}}
        </el-col>
        <el-col :span="8" v-else>
          <span>仓库：</span>
          <el-select v-model="detailData.warehouse" placeholder="请选择" style="width:100%;" disabled v-if="detailData.miningBySales == '1' && detailData.dropShipping == '1'">
            <el-option v-for="(item,index) of warehouseList" :key="index" :label="item.name" :value="item.warehouseId"></el-option>
          </el-select>
          <el-select v-model="detailData.warehouse" placeholder="请选择" style="width:100%;" :disabled="disabled" v-else>
            <el-option v-for="(item,index) of warehouseList" :key="index" :label="item.name" :value="item.warehouseId"></el-option>
          </el-select>
        </el-col> -->
      </el-row>
      <el-row class="detail-item">
        <el-col :span="8">
          <span>发票类型：</span>{{ detailData.invoiceType ?  detailData.invoiceType : '暂无'}}
        </el-col>
        <el-col :span="8">
          <span>入库员：</span>{{ detailData.warehouseStaff ?  detailData.warehouseStaff : '暂无'}}
        </el-col>
      </el-row>
      <el-row class="detail-item">
        <el-col :span="8">
          <span>状态：</span>
          <font v-if="detailData.status == 0" style="color:#13C2C2">待入库</font>
          <font v-if="detailData.status == 11" style="color:#13C2C2">已提交</font>
          <font v-if="detailData.status == 1" style="color:#52C41A">已入库</font>
          <font v-if="detailData.status == 4" style="color:#52C41A">部分入库</font>
          <font v-if="detailData.status == 3" style="color:#fd562c">拒绝</font>
        </el-col>
        <el-col :span="8">
          <span>审核状态：</span>
          <font v-if="detailData.approvalStatus == 0" style="color:#13C2C2">待审核</font>
          <font v-if="detailData.approvalStatus == 1" style="color:#52C41A">已审核</font>
          <font v-if="detailData.approvalStatus == 3" style="color:#fd562c">拒绝</font>
        </el-col>
      </el-row>
      <el-row class="detail-item">
        <el-col :span="24">
          <el-table :data="detailData.detailList" @row-click="scanCode">
            <el-table-column label="序号" type="index" align="center" width="50" />
            <el-table-column :label="item.label" :prop="item.prop" :width="item.width" v-for="(item, index) in columns" :key="index" align="center">
              <template slot-scope="scope">
                <template v-if="detailData.miningBySales == '0'">
                  <template v-if="detailData.status == '0' || detailData.status == '3'">
                    <template v-if="item.type == 'batch'">
                      <template v-if="scope.row.enableBatches == '0'">
                        <el-input placeholder="请输入" v-model="scope.row[item.prop]" @click.native.stop="stopScanCode" @blur="onBatchDataSave(scope.row)"  />
                      </template>
                      <template v-else>
                        <el-input placeholder="未启用批次" disabled />
                      </template>
                    </template>
                    <el-input placeholder="请输入" v-model="scope.row[item.prop]" @click.native.stop="stopScanCode" v-if="item.type == 'factoryBatch'" />
                    <!-- <el-input placeholder="请输入" v-model="scope.row[item.prop]" @click.native.stop="stopScanCode" @input="(val) => inputNum(val, scope.row)" v-if="item.type == 'input'" /> -->
                    <template v-if="item.type == 'endNum'">
                      <template v-if="detailData.orderType !== '1'">
                        <template v-if="scope.row.isSerial == '0'">
                          <el-input placeholder="扫码入库" v-model="scope.row[item.prop]" disabled />
                        </template>
                        <template v-else>
                          <el-input placeholder="请输入" v-model="scope.row[item.prop]" @click.native.stop="stopScanCode" @input="(val) => inputNum(val, scope.row, 'endNum')"  :disabled="scope.row.productMultiPositionList !== null" @blur="onNumDataSave(scope.row)" />
                        </template>
                      </template>
                      <template v-else>
                        <el-input placeholder="请输入" v-model="scope.row[item.prop]" @click.native.stop="stopScanCode" @input="(val) => inputNum(val, scope.row, 'endNum')"  :disabled="scope.row.productMultiPositionList !== null" @blur="onNumDataSave(scope.row)" />
                      </template>
                    </template>
                    <el-input placeholder="" v-model="scope.row[item.prop]" v-if="item.type == 'remark'" @click.native.stop="stopScanCode" />
                    <el-select v-model="scope.row[item.prop]" placeholder="请选择" style="width:100%;" v-if="item.type == 'warehouse'" :disabled="scope.row.productMultiPositionList !== null" @change="val => onSelectWarehouse(val, scope.row)">
                      <el-option v-for="(item,index) of warehouseList" :key="index" :label="item.name" :value="item.warehouseId"></el-option>
                    </el-select>
                    <el-input v-model="scope.row[item.prop]" v-if="item.type == 'area'" disabled placeholder="点击搜索按钮搜索" @click.native.stop="stopScanCode">
                      <el-button slot="append" icon="el-icon-search" @click.stop="onClickWarehouseList(scope.$index, scope.row)"></el-button>
                    </el-input>
                    <template v-if="item.type == 'plan'">{{ scope.row[item.prop] }}</template>
                    <span v-if="item.type == '1'">{{ scope.row[item.prop] }}</span>
                    <el-tooltip v-if="item.type == 'more'" effect="dark" content="多仓位" placement="bottom" >
                      <span class="el-icon-s-grid more" @click.stop="showMorePosition(scope.$index, scope.row)"></span>
                    </el-tooltip>
                  </template>
                  <template v-else>
                    <template v-if="scope.row.productMultiPositionList !== null">
                      <el-tooltip v-if="item.type == 'more'" effect="dark" content="多仓位" placement="bottom" >
                        <span class="el-icon-s-grid more" @click.stop="showMorePosition(scope.$index, scope.row)"></span>
                      </el-tooltip>
                    </template>
                    <el-select v-model="scope.row[item.prop]" placeholder="请选择" style="width:100%;" v-if="item.type == 'warehouse'" disabled>
                      <el-option v-for="(item,index) of warehouseList" :key="index" :label="item.name" :value="item.warehouseId"></el-option>
                    </el-select>
                    <span v-else>{{ scope.row[item.prop] }}</span>
                  </template>
                </template>
                <template v-else>
                  <template v-if="detailData.dropShipping == '0'">
                    <template v-if="detailData.status == '0' || detailData.status == '3'">
                      <template v-if="item.type == 'batch'">
                        <template v-if="scope.row.enableBatches == '0'">
                          <el-input placeholder="请输入" v-model="scope.row[item.prop]" @click.native.stop="stopScanCode" @blur="onBatchDataSave(scope.row)"  />
                        </template>
                        <template v-else>
                          <el-input placeholder="未启用批次" disabled />
                        </template>
                      </template>
                      <el-input placeholder="请输入" v-model="scope.row[item.prop]" @click.native.stop="stopScanCode" v-if="item.type == 'factoryBatch'" />
                      <!-- <el-input placeholder="请输入" v-model="scope.row[item.prop]" @click.native.stop="stopScanCode" @input="(val) => inputNum(val, scope.row)" v-if="item.type == 'input'" /> -->
                      <template v-if="item.type == 'endNum'">
                        <template v-if="scope.row.isSerial == '0'">
                          <el-input placeholder="扫码入库" v-model="scope.row[item.prop]" disabled />
                        </template>
                        <template v-else>
                          <el-input placeholder="请输入" v-model="scope.row[item.prop]" @click.native.stop="stopScanCode" @input="(val) => inputNum(val, scope.row, 'endNum')"  :disabled="scope.row.productMultiPositionList !== null" @blur="onNumDataSave(scope.row)" />
                        </template>
                      </template>
                      <el-input placeholder="" v-model="scope.row[item.prop]" v-if="item.type == 'remark'" @click.native.stop="stopScanCode" />
                      <el-select v-model="scope.row[item.prop]" placeholder="请选择" style="width:100%;" v-if="item.type == 'warehouse'" :disabled="scope.row.productMultiPositionList !== null" @change="val => onSelectWarehouse(val, scope.row)">
                        <el-option v-for="(item,index) of warehouseList" :key="index" :label="item.name" :value="item.warehouseId"></el-option>
                      </el-select>
                      <el-input v-model="scope.row[item.prop]" v-if="item.type == 'area'" disabled placeholder="点击搜索按钮搜索" @click.native.stop="stopScanCode">
                        <el-button slot="append" icon="el-icon-search" @click.stop="onClickWarehouseList(scope.$index, scope.row)"></el-button>
                      </el-input>
                      <template v-if="item.type == 'plan'">{{ scope.row[item.prop] }}</template>
                      <span v-if="item.type == '1'">{{ scope.row[item.prop] }}</span>
                      <el-tooltip v-if="item.type == 'more'" effect="dark" content="多仓位" placement="bottom" >
                        <span class="el-icon-s-grid more" @click.stop="showMorePosition(scope.$index, scope.row)"></span>
                      </el-tooltip>
                    </template>
                    <template v-else>
                      <template v-if="scope.row.productMultiPositionList !== null">
                        <el-tooltip v-if="item.type == 'more'" effect="dark" content="多仓位" placement="bottom" >
                          <span class="el-icon-s-grid more" @click.stop="showMorePosition(scope.$index, scope.row)"></span>
                        </el-tooltip>
                      </template>
                      <el-select v-model="scope.row[item.prop]" placeholder="请选择" style="width:100%;" v-if="item.type == 'warehouse'" disabled>
                        <el-option v-for="(item,index) of warehouseList" :key="index" :label="item.name" :value="item.warehouseId"></el-option>
                      </el-select>
                      <span v-else>{{ scope.row[item.prop] }}</span>
                    </template>
                  </template>
                  <template v-else>
                    <template v-if="scope.row.productMultiPositionList !== null">
                        <el-tooltip v-if="item.type == 'more'" effect="dark" content="多仓位" placement="bottom" >
                          <span class="el-icon-s-grid more" @click.stop="showMorePosition(scope.$index, scope.row)"></span>
                        </el-tooltip>
                      </template>
                      <el-select v-model="scope.row[item.prop]" placeholder="请选择" style="width:100%;" v-if="item.type == 'warehouse'" disabled>
                        <el-option v-for="(item,index) of warehouseList" :key="index" :label="item.name" :value="item.warehouseId"></el-option>
                      </el-select>
                      <span v-else>{{ scope.row[item.prop] }}</span>
                  </template>
                </template>
              </template>
            </el-table-column>
            <template v-if="detailData.invoiceType !== '收据' && detailData.invoiceType !== null && detailData.invoiceType !== ''">
              <el-table-column label="发票号" align="center" prop="invoice" width="120">
                <template slot-scope="scope">
                  <el-input placeholder="" v-model="scope.row.invoice" @blur="onInvoiceSave(scope.row)" @click.native.stop="stopScanCode" />
                </template>
              </el-table-column>
            </template>
          </el-table>
        </el-col>
      </el-row>
    </div>
    <!-- 审核/反审核 -->
    <el-dialog :visible.sync="open" width="500" top="100px" append-to-body title="审核意见" center>
      <el-row>
        <el-col :span="24">
          <textarea
            class="approve-textarea"
            v-model="approveRemark"
            type="textarea"
            placeholder="请输入意见和看法"
          >
          </textarea>
        </el-col>
      </el-row>
      <div slot="footer" class="dialog-footer">
        <el-button
          type="primary"
          class="btn-success mr20 width100"
          @click="agreeApprove()"
        >同意</el-button>
        <el-button
          type="primary"
          class="btn-cancel width100"
          @click="disagreeApprove()"
        >拒绝</el-button>
      </div>
    </el-dialog>
    <!-- 多仓位 -->
    <el-dialog :visible.sync="morePosition" width="800px" top="100px" append-to-body center :close-on-click-modal='false' :close-on-press-escape='false' :show-close="false">
      <el-row class="mb20">
        <el-col :span="20" style="line-height:34px;">
          <span>待入库数量：</span>{{ planNum }}
        </el-col>
        <el-col :span="4" v-if="closeScanMore == true">
          <ul class="scan">
            <li @click="onCloseMoreScanCode" class="close">删除扫码</li>
          </ul>
        </el-col>
      </el-row>
      <el-table :data="moreData" @row-click="scanCodeMorePosition">
        <el-table-column label="名称" align="center" prop="productName" width="150" />
        <el-table-column label="订单数量" align="center" prop="purchaseNum" width="100" />
        <el-table-column label="实际数量" align="center" prop="endNum" width="100">
          <template slot-scope="scope">
            <template v-if="scope.row.isSerial == '0'">
              <el-input placeholder="扫码入库" v-model="scope.row.endNum" @click.native.stop="stopScanCode" disabled />
            </template>
            <template v-else>
              <el-input placeholder="请输入" v-model="scope.row.endNum" @click.native.stop="stopScanCode" @input="(val) => inputEditNum(val, scope.row)" @blur="(val) => onBlur(val, scope.row)" :disabled="detailData.status == '4' || detailData.status == '1'" />
            </template>
          </template>
        </el-table-column>
        <el-table-column label="区域" align="center" prop="areaName">
          <template slot-scope="scope">
            <el-input v-model="scope.row.areaName" disabled placeholder="点击按钮搜索" @click.native.stop="stopScanCode">
              <el-button slot="append" icon="el-icon-search" @click.stop="morePositionWarehouseList(scope.row, scope.$index)"></el-button>
            </el-input>
          </template>
        </el-table-column>
        <el-table-column label="仓位" align="center" prop="positionName">
          <template slot-scope="scope">
            <el-input v-model="scope.row.positionName" disabled placeholder="点击按钮搜索" @click.native.stop="stopScanCode">
              <el-button slot="append" icon="el-icon-search" @click.stop="morePositionWarehouseList(scope.row, scope.$index)"></el-button>
            </el-input>
          </template>
        </el-table-column>
        <el-table-column label="" align="center" width="90px" class-name="small-padding fixed-width" v-if="detailData.status !== '4' && detailData.status !== '1'">
          <template slot-scope="scope">
            <span class="el-icon-circle-plus-outline icon-more-position" @click.stop="onAddMorePosition(scope.row)"></span>
            <span class="el-icon-circle-close icon-more-position" @click.stop="onDeleteMorePosition(scope.row)" v-if="scope.row.isSerial !== '0' && scope.$index !== 0"></span>
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" class="btn-success width100" @click="onHideMorePosition">关闭</el-button>
      </div>
    </el-dialog>
    <!-- 扫码框 -->
    <!-- class="input-data" -->
    <input v-model="scanCodeData.code"
      class="input-data"
      ref="getFocus"
      type="password"
      @keydown.enter="numberEnterDown()"
      @keyup.enter="numberEnterUp()"
    />
    <!-- 打印 -->
    <div ref="print" style="position:absolute;left:0;right:0;z-index:-999;">
      <h1 style="font-size:20px;text-align:center;margin-bottom:20px;">入库单</h1>
      <p style="display:flex;font-size:14px;line-height:40px;">
        <span style="flex:1;align-items:flex-end;">入库单号：{{ detailData.code }}</span>
        <span style="flex:1;align-items:flex-end;">入库日期：{{ detailData.warehouseTime }}</span>
      </p>
      <p style="display:flex;font-size:14px;line-height:40px;">
        <span style="flex:1;align-items:flex-end;">源采购单：{{ detailData.purchaseOrderCode }}</span>
        <span style="flex:1;align-items:flex-end;">单据状态：{{ detailData.status == '0' ? '待入库' : detailData.status == '11' ? '已提交' : detailData.status == '1' ? '已入库' : detailData.status == '4' ? '部分完成' : '拒绝' }}</span>
      </p>
      <p style="display:flex;font-size:14px;line-height:40px;">
        <span style="flex:1;align-items:flex-end;">入库员：{{ detailData.warehouseStaff }}</span>
      </p>
      <table style="font-size:14px;margin-top:10px;width:100%;border:1px solid #000;border-collapse:collapse;border-top:none;border-right:none;">
        <tr>
          <th width="15%" style="border:1px solid #000;border-left:none;padding:10px 0;">产品编码</th>
          <th width="20%" style="border:1px solid #000;border-left:none;padding:10px 0;">产品名称</th>
          <th width="10%" style="border:1px solid #000;border-left:none;padding:10px 0;">厂家批次</th>
          <th width="15%" style="border:1px solid #000;border-left:none;padding:10px 0;">规格</th>
          <th width="10%" style="border:1px solid #000;border-left:none;padding:10px 0;">订单数量</th>
          <th width="10%" style="border:1px solid #000;border-left:none;padding:10px 0;">实际入库数量</th>
          <th width="10%" style="border:1px solid #000;border-left:none;padding:10px 0;">单位</th>
          <th width="10%" style="border:1px solid #000;border-left:none;padding:10px 0;">仓库</th>
        </tr>
        <tr v-for="item in detailData.detailList" :key="item.id">
          <td width="15%" style="text-align:center;padding:10px;border-right:1px solid #000;border-bottom:1px solid #000;">{{ item.productBasicCode }}</td>
          <td width="20%" style="text-align:center;padding:10px;border-right:1px solid #000;border-bottom:1px solid #000;">{{ item.productBasicName }}</td>
          <td width="10%" style="text-align:center;padding:10px;border-right:1px solid #000;border-bottom:1px solid #000;">{{ item.factoryBatch }}</td>
          <td width="15%" style="text-align:center;padding:10px;border-right:1px solid #000;border-bottom:1px solid #000;">{{ item.specification }}</td>
          <td width="10%" style="text-align:center;padding:10px;border-right:1px solid #000;border-bottom:1px solid #000;">{{ item.purchaseNum }}</td>
          <td width="10%" style="text-align:center;padding:10px;border-right:1px solid #000;border-bottom:1px solid #000;">{{ item.endNum }}</td>
          <td width="10%" style="text-align:center;padding:10px;border-right:1px solid #000;border-bottom:1px solid #000;">{{ item.unit }}</td>
          <td width="10%" style="text-align:center;padding:10px;border-right:1px solid #000;border-bottom:1px solid #000;">{{ item.warehouse }}</td>
        </tr>
      </table>
    </div>
  </div>
</template>

<script>
import { storageDetail, storageConfirm, storageAudit, storageMorePosition, storageAddMorePosition, storageAddMorePositionData, storageEditMorePosition, storageUpdateMorePosition, storageScanCode, storageDeleteScanCode, storageDeleteMorePosition, storageEdit, delMorePosition } from "@/api/system/purchaseStorage/storage"
import { listConfig, detailConfig } from "@/api/system/setting/config/config"
import { queryWarehouse } from "@/api/system/warehouse/warehouse"
export default {
  name: "storageDetail",
  data(){
    return {
      columns: [{
        prop: "warehouse",
        label: "仓库",
        width: 150,
        type: 'warehouse'
      },{
        prop: "productBasicCode",
        label: "产品编码",
        width: 150,
        type: '1'
      },{
        prop: "productBasicName",
        label: "产品名称",
        width: 200,
        type: '1'
      },{
        prop: "productName",
        label: "SKU名称",
        width: 200,
        type: '1'
      },{
        prop: "specification",
        label: "规格",
        width: 100,
        type: '1'
      },{
        prop: "batch",
        label: "批次号",
        width: 150,
        type: 'batch'
      },{
        prop: "purchaseNum",
        label: "订单数量",
        width: 120,
        type: '1'
      },{
        prop: "planNum",
        label: "待入库数量",
        width: 120,
        type: '1'
      },
      // {
      //   prop: "grossWeight",
      //   label: "毛重",
      //   width: 100,
      //   type: 'input'
      // },{
      //   prop: "hideWeight",
      //   label: "皮重",
      //   width: 100,
      //   type: 'input'
      // },
      {
        prop: "unit",
        label: "单位",
        width: 100,
        type: '1'
      },{
        prop: "endNum",
        label: "实际入库",
        width: 110,
        type: 'endNum'
      },{
        prop: "factoryBatch",
        label: "厂家批次",
        width: 150,
        type: 'factoryBatch'
      },{
        prop: "areaName",
        label: "区域",
        width: 200,
        type: 'area'
      },{
        prop: "positionName",
        label: "仓位",
        width: 200,
        type: 'area'
      },{
        prop: "remark",
        label: "备注",
        width: 200,
        type: 'remark'
      },{
        label: "",
        width: 50,
        type: 'more'
      }],
      id: '',
      detailData: '',
      warehouseList: [], // 仓库
      purInboundNoCheck: {}, // 采购入库免提交
      purInboundOverStocking: {}, // 采购入库超量入库
      purInboundSplitOrder: {}, // 采购入库拆单
      open: false,
      approveRemark: '',
      morePosition: false,
      moreData: [],
      planNum: '',
      pendingStorage: '',
      morePositionData: '',
      disabled: false,
      closeScan: false,
      closeScanMore: false,
      type: '1',
      scanCodeData: {
        code: '',
        pendingStorage: ''
      },
      closeScanCodeData: {
        code: '',
        stockType: ''
      },
      index: ''
    }
  },
  created() {
    this.id = this.$route.query.id
    if(this.$route.query.data){
      // console.log(this.$route.query.data)
      this.detailData = this.$route.query.data
      let index  = this.$route.query.index
      let obj = {
        id: this.detailData.detailList[index].id,
        warehouse: this.detailData.detailList[index].warehouse,
        areaId: this.detailData.detailList[index].areaId,
        areaName: this.detailData.detailList[index].areaName,
        positionId: this.detailData.detailList[index].positionId,
        positionName: this.detailData.detailList[index].positionName,
      }
      storageEdit(obj).then(res => {
        if(res.code == 200){
          this.getDetailData()
          this.getWarehouseList()
          this.$router.push({ query: {id: this.id} })
        }
      })
    }else{
      this.getDetailData()
    }
    if(this.$route.query.list){
      let list = this.$route.query.list
      let index = this.$route.query.index
      this.index = this.$route.query.index
      let planNum = this.$route.query.planNum
      let pendingStorage = this.$route.query.pendingStorage
      storageMorePosition(list.businessId).then(res => {
        if(res.code == 200){
          this.morePosition = true
          this.planNum = planNum
          this.pendingStorage = pendingStorage
          this.moreData = res.rows
          this.moreData[index] = list
          this.moreData[index].oldPlanNum = this.planNum
          this.getStorageUpdateMorePosition(this.moreData[index])
          this.$router.push({ query: {id: this.id} })
        }
      })
    }
    this.getConfig()
  },
  methods: {
    // 获取参数配置详情
    getConfig(){
      listConfig().then(res => {
        this.purInboundNoCheck = res.data.find(item => item.modelId == 3 && item.typeId == 12)
      })
    },
    // 获取详细信息
    getDetailData(){
      storageDetail(this.id).then(res => {
        if(res.data.warehouse !== null){
          this.disabled = true
        }
        res.data.detailList.forEach(item => {
          if(item.endNum == 0 || item.endNum == null){
            item.endNum = ''
          }
          if(item.grossWeight == 0){
            item.grossWeight = ''
          }
          if(item.hideWeight == 0){
            item.hideWeight = ''
          }
          if(item.enableBatches == '1'){
            item.batch = ''
          }
        })
        this.detailData = res.data
        this.getWarehouseList()
      })
    },
    // 获取仓库列表
    getWarehouseList(){
      queryWarehouse().then(res => {
        if(res.code == 200){
          this.warehouseList = res.data
          if(this.detailData.status == '1' || this.detailData.status == '11'){
            // 主表仓库id转换
            // let arr = res.data.find(e => {
            //   return e.warehouseId == this.detailData.warehouse
            // })
            // if(arr != undefined){
            //   this.detailData.warehouse = arr.name
            // }
            // 产品仓库id转换
            this.detailData.detailList.forEach(i => {
              // console.log(i)
              let dataList = res.data.find(item => {
                return item.warehouseId == i.warehouse
              })
              if(dataList != undefined){
                i.warehouse = dataList.name
              }
            })
          }else{
            if(this.detailData.orderType !== '1'){
              // if(this.detailData.warehouse == null || this.detailData.warehouse == ''){
              //   if(res.data[0].warehouseId){
              //     this.detailData.warehouse = res.data[0].warehouseId
              //   }
              // }
              // this.detailData.detailList.forEach(item => {
              //   if(item.endNum == ''){
              //     if(item.warehouse == null || item.warehouse == ''){
              //       return item.warehouse = res.data[0].warehouseId
              //     }
              //   }
              // })
            }
          }
        }
      })
    },
    // 产品仓库选择
    onSelectWarehouse(val, row){
      row.endNum = 0
      storageEdit({id: row.id, warehouse: row.warehouse, endNum: row.endNum, pendingStorage: row.pendingStorage, updateWarehouse: 1 }).then(res => {
        if(res.code == 200){
          this.getDetailData()
        }
      })
      // 当前选中仓库对应仓库列表的数据
      let isWarehouse = this.warehouseList.find(item => item.warehouseId == row.warehouse)
      if(isWarehouse.openPosition !== '0'){
        storageMorePosition(row.id).then(res => {
          // 判断多仓位
          if(res.rows.length !== 0){
            storageDeleteMorePosition({businessId: row.id, businessType: 0}).then(res => {
              if(res.code == 200){
                // this.$modal.msgError('未启用仓位管理！')
              }
            })
          }
        })
      }
    },
    // 单仓位扫码
    scanCode(row){
      // console.log(row)
      // 判断是否已提交
      if(row.warehouse == null){
        this.$modal.msgError('请选择仓库!')
      }else{
      // 当前选中仓库对应仓库列表的数据
        let isWarehouse = this.warehouseList.find(item => item.warehouseId == row.warehouse)
        if(this.detailData.status !== '11' && this.detailData.status !== '1' && this.detailData.status !== '4'){
          storageMorePosition(row.id).then(res => {
            if(row.isSerial == '0'){
              // 判断是否启用仓位管理
              if(isWarehouse.openPosition == '0'){
                if(row.areaId !== null){
                  this.getScanCode(row)
                }else{
                  this.closeScan = false
                  this.$modal.msgError('请选择仓库、区域、仓位!')
                }
              }else{
                this.getScanCode(row)
              }
            }else{
              this.closeScan = false
              this.scanCodeData = {}
              this.$modal.msgError('未启用扫码!')
            }
          })
        }
      }
    },
    // 开始扫码
    getScanCode(row){
      // console.log(row)
      this.$refs.getFocus.focus()
      this.closeScan = true
      if(row.endNum !== ''){
        this.scanCodeData = {
          purInboundOrderDetailId: row.id,
          // businessId: row.id,
          stockType: '1',
          pendingStorage: row.planNum,
          planNum: row.pendingStorage
        }
      }else{
        this.scanCodeData = {
          purInboundOrderDetailId: row.id,
          // businessId: row.id,
          stockType: '1',
          pendingStorage: row.planNum,
          planNum: row.pendingStorage,
          warehouse: row.warehouse,
          areaId: row.areaId,
          areaName: row.areaName,
          positionId: row.positionId,
          positionName: row.positionName
        }
      }
      this.type = '1'
      this.$modal.msgSuccess("开始扫码！")
    },
    // 多仓位扫码
    scanCodeMorePosition(row){
      if(this.detailData.status !== '4' && this.detailData.status !== '1'){
        if(row.isSerial == '0'){
          if(row.areaId !== null){
            this.$refs.getFocus.focus()
            this.closeScanMore = true
            if(row.endNum !== ''){
              this.scanCodeData = {
                purInboundOrderDetailId: row.businessId,
                businessId: row.id,
                stockType: '2',
                pendingStorage: this.planNum
              }
            }else{
              this.scanCodeData = {
                purInboundOrderDetailId: row.businessId,
                businessId: row.id,
                stockType: '2',
                pendingStorage: this.planNum,
                warehouse: row.warehouse,
                areaId: row.areaId,
                areaName: row.areaName,
                positionId: row.positionId,
                positionName: row.positionName
              }
            }
            this.morePositionData = row
            this.type = '1'
            this.$modal.msgSuccess("开始扫码！")
          }else{
            this.closeScanMore = false
            this.$modal.msgError('请选择仓库、区域、仓位!')
          }
        }else{
          this.closeScanMore = false
          this.scanCodeData = {}
          this.$modal.msgError('未启用扫码!')
        }
      }
    },
    onBlurScanCode(){
      this.closeScan = false
      this.closeScanMore = false
    },
    numberEnterDown() {
      this.keyDownTime = new Date().getTime()
    },
    numberEnterUp() {
      this.keyUpTime = new Date().getTime()
      if (this.keyUpTime - this.keyDownTime < 30) {
        // 扫码枪触发
        // let newCode = this.common.codeRule(this.scanCodeData.code)
        let data = this.common.codeBoxRule(this.scanCodeData.code)
        if(data.code.length == 20){
          if(this.type == '1'){
            // 扫码入库
            this.scanCodeData.code = data.code
            this.scanCodeData.productionDate = data.productionDate
            this.scanCodeData.expirationDate = data.expirationDate
            storageScanCode(this.scanCodeData).then(res => {
              if(res.code == 200){
                this.getDetailData()
                this.scanCodeData.code = ''
                this.scanCodeData.productionDate = ''
                this.scanCodeData.expirationDate = ''
                this.scanCodeData.pendingStorage = res.data.pendingStorage
                if(this.scanCodeData.stockType == '2'){
                  this.planNum = res.data.pendingStorage
                  storageMorePosition(this.morePositionData.businessId).then(res => {
                    this.moreData = res.rows
                  })
                }
                this.$refs.getFocus.focus()
                this.$modal.msgSuccess("扫码成功")
              }else{
                this.scanCodeData.code = ''
                this.scanCodeData.productionDate = ''
                this.scanCodeData.expirationDate = ''
                this.$refs.getFocus.focus()
                this.$modal.msgError(res.msg)
              }
            })
          }else{
            // 删除扫码
            this.closeScanCodeData.code = data.code
            storageDeleteScanCode(this.closeScanCodeData).then(res => {
              if(res.code == 200){
                this.getDetailData()
                this.scanCodeData.code = ''
                if(this.scanCodeData.stockType == '2'){
                  this.planNum = res.data.pendingStorage
                  // console.log(this.morePositionData)
                  storageMorePosition(this.morePositionData.businessId).then(res => {
                    this.moreData = res.rows
                  })
                }
                this.$refs.getFocus.focus()
                this.$modal.msgSuccess("删除成功")
              }else{
                this.closeScanCodeData.code = ''
                this.$refs.getFocus.focus()
                this.$modal.msgError(res.msg)
              }
            })
          }
        }else{
          this.scanCodeData.code = ''
          this.$refs.getFocus.focus()
          this.$modal.msgError('扫码错误，请重新扫码!')
        }
      }else{
        // 直接按键盘触发
        // let newCode = this.common.codeRule(this.scanCodeData.code)
        let data = this.common.codeBoxRule(this.scanCodeData.code)
        if(data.code.length == 20){
          if(this.type == '1'){
            // 扫码入库
            this.scanCodeData.code = data.code
            this.scanCodeData.productionDate = data.productionDate
            this.scanCodeData.expirationDate = data.expirationDate
            storageScanCode(this.scanCodeData).then(res => {
              if(res.code == 200){
                this.getDetailData()
                this.scanCodeData.code = ''
                this.scanCodeData.productionDate = ''
                this.scanCodeData.expirationDate = ''
                this.scanCodeData.pendingStorage = res.data.pendingStorage
                if(this.scanCodeData.stockType == '2'){
                  this.planNum = res.data.pendingStorage
                  storageMorePosition(this.morePositionData.businessId).then(res => {
                    this.moreData = res.rows
                  })
                }
                this.$refs.getFocus.focus()
                this.$modal.msgSuccess("扫码成功")
              }else{
                this.scanCodeData.code = ''
                this.scanCodeData.productionDate = ''
                this.scanCodeData.expirationDate = ''
                this.$refs.getFocus.focus()
                this.$modal.msgError(res.msg)
              }
            })
          }else{
            // 删除扫码
            this.closeScanCodeData.code = data.code
            storageDeleteScanCode(this.closeScanCodeData).then(res => {
              if(res.code == 200){
                this.getDetailData()
                this.scanCodeData.code = ''
                if(this.scanCodeData.stockType == '2'){
                  this.planNum = res.data.pendingStorage
                  storageMorePosition(this.morePositionData.businessId).then(res => {
                    this.moreData = res.rows
                  })
                }
                this.$refs.getFocus.focus()
                this.$modal.msgSuccess("删除成功")
              }else{
                this.closeScanCodeData.code = ''
                this.$refs.getFocus.focus()
                this.$modal.msgError(res.msg)
              }
            })
          }
        }else{
          this.scanCodeData.code = ''
          this.$refs.getFocus.focus()
          this.$modal.msgError('扫码错误，请重新扫码!')
        }
      }
    },
    // 单仓位删除扫码
    onCloseScanCode(){
      this.closeScanCodeData.code = ''
      this.closeScanCodeData.stockType = '1'
      this.closeScanCodeData.purInboundOrderDetailId = this.scanCodeData.purInboundOrderDetailId
      this.type = '2'
      this.$refs.getFocus.focus()
      this.$modal.msgSuccess('删除扫码!')
    },
    // 多仓位删除扫码
    onCloseMoreScanCode(){
      this.closeScanCodeData.code = ''
      this.closeScanCodeData.stockType = '2'
      this.closeScanCodeData.purInboundOrderDetailId = this.scanCodeData.purInboundOrderDetailId
      this.type = '2'
      this.$refs.getFocus.focus()
      this.$modal.msgSuccess('删除扫码!')
    },
    // 阻止事件
    stopScanCode(){},
    // 跳转仓库列表页面
    onClickWarehouseList(index, row){
      if(row.warehouse == null){
        this.$modal.msgError('请选择仓库!')
      }else{
        // 当前选中仓库对应仓库列表的数据
        let isWarehouse = this.warehouseList.find(item => item.warehouseId == row.warehouse)
        // 判断是否启用仓位管理
        if(isWarehouse.openPosition == '0'){
          if(row.endNum !== ''){
            this.$modal.msgError('实际入库已存在，不能编辑!')
          }else{
            storageMorePosition(row.id).then(res => {
              if(res.rows.length > 0){
                this.$modal.msgError('已存在多仓位，请去多仓位编辑!')
              }else{
                if(this.detailData.detailList[index].warehouse !== null){
                  this.$router.push({path:"/warehouseManagement/warehouse/list", query:{ type: 'purchaseStorage', data: this.detailData, index: index }})
                }else{
                  this.$modal.msgError('请选择仓库！')
                }
              }
            })
          }
        }else{
          this.$modal.msgError('未启用仓位管理！')
        }
      }
    },
    // 数量验证
    inputNum(val, row, item){
      if(item == 'endNum'){
         // 当前选中仓库对应仓库列表的数据
        let isWarehouse = this.warehouseList.find(item => item.warehouseId == row.warehouse)
        // 判断是否启用仓位管理
        if(isWarehouse.openPosition == '0'){
          if(row.areaId !== null){
            storageMorePosition(row.id).then(res => {
              if(res.rows.length > 0){
                row.endNum = ''
                this.$modal.msgError('已存在多仓位，请去多仓位编辑!')
              }else{
                if(val == '' || val == undefined){
                  this.$modal.msgError('不能为空!')
                }else{
                  if(/^[1-9]\d*$|^0$/.test(val) == false){
                    this.$modal.msgError('必须是数字！')
                    row.endNum = ''
                    return false
                  }else{
                    if(JSON.parse(val) <= 0){
                      row.endNum = ''
                      this.$modal.msgError('必须大于0!')
                    }else{
                      if(this.detailData.miningBySales == '0'){
                        detailConfig('overStocking', 'purInbound').then(res => {
                          let ifAllow = res.data[0].ifAllow
                          if(ifAllow == '1'){
                            if(JSON.parse(val) > row.pendingStorage){
                              row.endNum = ''
                              this.$modal.msgError('不能超量入库，请重新输入!')
                            }
                          }
                        })
                      }else{
                        if(this.detailData.dropShipping == '1'){
                          if(JSON.parse(val) > row.pendingStorage){
                            row.endNum = ''
                            this.$modal.msgError('不能大于出库数量，请重新输入!')
                          }
                        }
                      }
                    }
                  }
                }
              }
            })
          }else{
            row.endNum = ''
            this.$modal.msgError('请选择仓位!')
          }
        }else{
          if(val == '' || val == undefined){
            this.$modal.msgError('不能为空!')
          }else{
            if(/^[1-9]\d*$|^0$/.test(val) == false){
              this.$modal.msgError('必须是数字！')
              row.endNum = ''
              return false
            }else{
              if(JSON.parse(val) <= 0){
                row.endNum = ''
                this.$modal.msgError('必须大于0!')
              }else{
                if(this.detailData.miningBySales == '0'){
                  detailConfig('overStocking', 'purInbound').then(res => {
                    let ifAllow = res.data[0].ifAllow
                    if(ifAllow == '1'){
                      if(JSON.parse(val) > row.pendingStorage){
                        row.endNum = ''
                        this.$modal.msgError('不能超量入库，请重新输入!')
                      }
                    }
                  })
                }else{
                  if(this.detailData.dropShipping == '0'){
                    if(JSON.parse(val) > row.pendingStorage){
                      row.endNum = ''
                      this.$modal.msgError('不能大于出库数量，请重新输入!')
                    }
                  }
                }
              }
            }
          }
        }
      }
    },
    // 批次号保存
    onBatchDataSave(row){
      if(row.batch !== '' && row.batch !== null){
        storageEdit({id: row.id, batch: row.batch, purOrderProductId: row.purOrderProductId}).then(res => {
          if(res.code == 200){
            this.getDetailData()
          }
        })
      }
    },
    // 数字保存
    onNumDataSave(row){
      console.log(row)
      if(row.endNum !== '' && row.endNum !== null){
        storageEdit({id: row.id, endNum: row.endNum, pendingStorage: row.pendingStorage}).then(res => {
          if(res.code == 200){
            this.getDetailData()
          }
        })
      }
    },
    // 多仓位
    showMorePosition(index, row){
      // 当前选中仓库对应仓库列表的数据
      // console.log(row)
      if(row.warehouse !== '' || row.warehouse !== null){
        let isWarehouse = ''
        if(typeof(row.warehouse) == 'string'){
          isWarehouse = this.warehouseList.find(item => item.name == row.warehouse)
        }else{
          isWarehouse = this.warehouseList.find(item => item.warehouseId == row.warehouse)
        }
        // let isWarehouse = this.warehouseList.find(item => item.warehouseId == row.warehouse)
        // 判断是否启用仓位管理
        if(isWarehouse.openPosition == '0'){
          if(row.areaId !== '' && row.areaId !== null){
            this.closeScan = false
            storageMorePosition(row.id).then(res => {
              if(res.code == 200){
                if(res.rows.length > 0 && row.endNum > 0){
                  this.morePosition = true
                  this.planNum = row.planNum
                  this.pendingStorage = row.pendingStorage
                  res.rows.forEach(item => {
                    if(item.endNum == 0){
                      return item.endNum = ''
                    }
                  })
                  this.moreData = res.rows
                }else{
                  this.morePositionData = row
                  this.getStorageMorePosition(this.morePositionData)
                }
              }
            })
          }else{
            this.$modal.msgError('请选择仓位！')
          }
        }else{
          this.$modal.msgError('未启用仓位管理！')
        }
      }else{
        this.$modal.msgError('请选择仓库！')
      }
    },
    // 获取多仓位列表
    getStorageMorePosition(row){
      // console.log(row)
      storageMorePosition(row.id).then(res => {
        if(res.code == 200){
          this.morePosition = true
          this.planNum = row.planNum
          this.pendingStorage = row.pendingStorage
          if(res.rows.length == 0 && row.endNum > 0){
            let obj = {
              businessId: row.id, // 采购单详情id
              productId: row.productId, // 产品id
              productCode: row.productCode, // 产品编码
              productName: row.productName, // 产品名称
              purchaseNum: row.purchaseNum, // 订单数量
              // oldPlanNum: row.planNum, // 待入库数量
              // planNum: row.planNum, // 计划入库数量
              endNum: row.endNum, // 实际数量
              status: row.status, // 状态
              warehouse: row.warehouse, // 仓库
              areaId: row.areaId, // 区域id
              areaName: row.areaName, // 区域名称
              positionId: row.positionId, // 仓位id
              positionName: row.positionName, // 仓位名称
              isSerial: row.isSerial,
              remark: row.remark
            }
            this.getStorageAddMorePosition(obj)
          }else if(res.rows.length >= 0 && row.endNum == '' ){
            let obj = {
              businessId: row.id, // 采购单详情id
              productId: row.productId, // 产品id
              productCode: row.productCode, // 产品编码
              productName: row.productName, // 产品名称
              purchaseNum: row.purchaseNum, // 订单数量
              // oldPlanNum: row.planNum, // 待入库数量
              // planNum: row.planNum, // 计划入库数量
              endNum: '', // 实际数量
              status: row.status, // 状态
              warehouse: row.warehouse, // 仓库
              areaId: row.areaId, // 区域id
              areaName: row.areaName, // 区域名称
              positionId: row.positionId, // 仓位id
              positionName: row.positionName, // 仓位名称
              isSerial: row.isSerial,
              remark: row.remark
            }
            this.getStorageAddMorePosition(obj)
          }else{
            res.rows.forEach(item => {
              if(item.endNum == 0){
                return item.endNum = ''
              }
            })
            this.moreData = res.rows
            // if(res.rows[0].endNum > 0){
            //   res.rows[0].oldPlanNum = this.planNum
            //   this.getStorageEditMorePosition(res.rows[0])
            // }
          }
        }
      })
    },
    // 多仓位新建
    getStorageAddMorePosition(e){
      storageAddMorePosition(e).then(res => {
        if(res.code == 200){
          if(e.endNum == ''){
            storageMorePosition(e.businessId).then(res => {
              res.rows.forEach(item => {
                if(item.endNum == 0){
                  return item.endNum = ''
                }
              })
              this.moreData = res.rows
            })
          }else{
            this.getStorageMorePosition(this.morePositionData)
          }
        }
      })
    },
    // 多仓位跳转仓库列表页面
    morePositionWarehouseList(row, index){
      if(this.detailData.status !== '4' && this.detailData.status !== '1'){
        // console.log(row)
        if(row.endNum > 0){
          this.$modal.msgError('实际入库已存在，不能编辑!')
        }else{
          this.$router.push({path:"/warehouseManagement/warehouse/list", query:{ type: 'purchaseStorageMorePosition', id: this.id, list: row, index: index, planNum: this.planNum, pendingStorage: this.pendingStorage }})
        }
      }
    },
    // 多仓位实际数量验证
    inputEditNum(val, row){
      // console.log(row)
      if(row.areaId !== null ){
        if(val == '' || val == undefined){
          this.$modal.msgError('实际数量不能为空!')
        }else{
          if(/^[1-9]\d*$|^0$/.test(val) == false){
            this.$modal.msgError('实际数量必须是数字！')
            row.endNum = ''
            return false
          }else{
            if(JSON.parse(val) <= 0){
              row.endNum = ''
              this.$modal.msgError('实际数量必须大于0!')
            }else{
              detailConfig('overStocking', 'purInbound').then(res => {
                let ifAllow = res.data[0].ifAllow
                if(ifAllow == '1'){
                  let num = 0
                  this.moreData.forEach(item => {
                    num += Number(item.endNum)
                    return num
                  })
                  if(num > this.pendingStorage){
                    row.endNum = ''
                    this.$modal.msgError('实际数量不能大于订单数量!')
                  }
                }
              })
            }
          }
        }
      }else{
        row.endNum = ''
        this.$modal.msgError('请选择区域、仓位!')
      }
    },
    // 多仓位数据保存
    onBlur(val, row){
      row.oldPlanNum = this.planNum
      if(val.target.value == null || val.target.value == ''){
        this.$modal.msgError('实际数量不能为空!')
      }else{
        this.getStorageEditMorePosition(row)
      }
    },
    getStorageUpdateMorePosition(e){
      storageUpdateMorePosition(e).then(res =>{
        if(res.code == 200){
          this.getDetailData()
          this.planNum = this.$route.query.planNum
        }else{
          this.$modal.msgError(res.msg)
          storageMorePosition(this.moreData[this.index].businessId).then(res => {
            if(res.code == 200){
              res.rows.forEach(item => {
                if(item.endNum == 0){
                  return item.endNum = ''
                }
              })
              this.moreData = res.rows
            }
          })
        }
      })
    },
    getStorageEditMorePosition(e){
      storageEditMorePosition(e).then(res => {
        if(res.code == 200){
          this.getDetailData()
          this.planNum = res.data.planNum
        }
      })
    },
    // 多仓位新建一行
    onAddMorePosition(row){
      let obj = {
        businessId: row.businessId, // 采购单详情id
        productId: row.productId, // 产品id
        productCode: row.productCode, // 产品编码
        productName: row.productName, // 产品名称
        purchaseNum: row.purchaseNum, // 订单数量
        endNum: '', // 实际数量
        status: row.status, // 状态
        warehouse: row.warehouse, // 仓库
        areaId: '', // 区域id
        areaName: '', // 区域名称
        positionId: '', // 仓位id
        positionName: '', // 仓位名称
        remark: ''
      }
      detailConfig('overStocking', 'purInbound').then(res => {
        let ifAllow = res.data[0].ifAllow
        if(ifAllow == '1'){
          let num = ''
          if(row.endNum == ''){
            num = 0
          }else{
            num = Number(row.endNum)
          }
          if(num < this.pendingStorage){
            storageAddMorePositionData(obj).then(res => {
              if(res.code == 200){
                storageMorePosition(row.businessId).then(res => {
                  res.rows.forEach(item => {
                    if(item.endNum == 0){
                      return item.endNum = ''
                    }
                  })
                  this.moreData = res.rows
                })
              }
            })
          }else{
            this.$modal.msgError('待入库数量为0,不能新建仓位！')
          }
        }else{
          storageAddMorePositionData(obj).then(res => {
            if(res.code == 200){
              storageMorePosition(row.businessId).then(res => {
                res.rows.forEach(item => {
                  if(item.endNum == 0){
                    return item.endNum = ''
                  }
                })
                this.moreData = res.rows
              })
            }
          })
        }
      })
    },
    // 多仓位删除一行
    onDeleteMorePosition(row){
      this.$modal.confirm('是否确认删除当前数据？', '删除').then(() => {
        delMorePosition(row.id).then(res => {
            if(res.code == 200){
                storageMorePosition(row.businessId).then(res => {
                  res.rows.forEach(item => {
                    if(item.endNum == 0){
                      return item.endNum = ''
                    }
                  })
                  this.moreData = res.rows
                })
            }
        })
      }).catch(() => {})
    },
    // 关闭多仓位
    onHideMorePosition(){
      this.morePosition = false
      this.closeScanMore = false
      this.moreData = []
      this.getDetailData()
    },
    // 票号保存
    onInvoiceSave(row){
      storageEdit({id: row.id, invoice: row.invoice}).then(res => {
        if(res.code == 200){
          this.getDetailData()
        }
      })
    },
    // 提交
    submit(){
      // 判断批次是否启用  启用必填
      let enableBatches = this.detailData.detailList.every(val => {
        if(val.enableBatches == '0'){
          return !!val.batch
        }else{
          return !val.batch
        }
      })
      // 判断实际入库不为空
      let index = this.detailData.detailList.findIndex(val => val.endNum !== '')
      let warehouse = this.detailData.detailList.every(val => !!val.warehouse)
      // console.log(index)
      if(warehouse == true){
        if(enableBatches == true){
          if(this.detailData.detailList.length > 1){
            if(index > -1){
              // 判断一条数据实际入库数量小于订单数量
              let num = this.detailData.detailList.every(val => {
                if(val.endNum < val.pendingStorage){
                  return false
                }else{
                  return true
                }
              })
              this.detailData.detailList.forEach(item => {
                if(item.endNum == ''){
                  item.endNum = 0
                }
              })
              let arr = this.detailData
              if(num == true){
                arr.status = '11'
                arr.approvalStatus = '0' // 待审核
                this.getStorageConfirm(arr)
              }else{
                detailConfig('splitOrder', 'purInbound').then(res => {
                  let ifAllow = res.data[0].ifAllow
                  if(ifAllow == '0'){
                    this.$modal.confirm('是否拆单？', '拆单').then(() => {
                      arr.status = '11'
                      arr.splitOrNo = '0' // 拆单
                      arr.approvalStatus = '0' // 待审核
                      this.getStorageConfirm(arr)
                    }).catch(action => {
                      if(action == 'cancel'){
                        arr.status = '11'
                        arr.splitOrNo = '1' // 不拆单
                        arr.approvalStatus = '0' // 待审核
                        this.getStorageConfirm(arr)
                      }
                    })
                  }else{
                    this.$modal.msgError('不允许拆单，请编辑实际入库！')
                  }
                })
              }
            }else{
              this.$modal.msgError('实际入库不能为空！')
            }
          }else{
            // 单条数据
            let endNum = this.detailData.detailList.every(val => !!val.endNum)
            if(endNum == true){
              this.detailData.detailList.forEach(item => {
                let arr = this.detailData
                if(item.endNum >= item.pendingStorage){
                  arr.status = '11'
                  arr.approvalStatus = '0' // 待审核
                  this.getStorageConfirm(arr)
                }else if(item.endNum < item.pendingStorage){
                  // 是否允许拆单
                  detailConfig('splitOrder', 'purInbound').then(res => {
                    let ifAllow = res.data[0].ifAllow
                    if(ifAllow == '0'){
                      this.$modal.confirm('是否拆单？', '拆单').then(() => {
                        arr.status = '11'
                        arr.splitOrNo = '0' // 拆单
                        arr.approvalStatus = '0' // 待审核
                        this.getStorageConfirm(arr)
                      }).catch(action => {
                        if(action == 'cancel'){
                          arr.status = '11'
                          arr.splitOrNo = '1' // 不拆单
                          arr.approvalStatus = '0' // 待审核
                          this.getStorageConfirm(arr)
                        }
                      })
                    }else{
                      this.$modal.msgError('不允许拆单，请编辑实际入库！')
                    }
                  })
                }
              })
            }else{
              this.$modal.msgError('实际入库不能为空！')
            }
          }
        }else{
          this.$modal.msgError('批次号不能为空！')
        }
      }else{
        this.$modal.msgError('仓库不能为空！')
      }
    },
    // 审核
    audit(){
      this.open = true
    },
    // 同意
    agreeApprove(){
      queryWarehouse().then(res => {
        if(res.code == 200){
          // 转换仓库
          let arr = res.data.find(e => {
            return e.name == this.detailData.warehouse
          })
          if(arr != undefined){
            this.detailData.warehouse = arr.warehouseId
          }
          this.detailData.detailList.forEach(i => {
            let dataList = res.data.find(item => {
              return item.name == i.warehouse
            })
            if(dataList != undefined){
              i.warehouse = dataList.warehouseId
            }
          })
          this.detailData.detailList.forEach(item => {
            if(item.endNum == ''){
              item.endNum = 0
            }
          })
          let data = this.detailData
          // console.log(data)
          data.oldApprovalStatus = this.detailData.approvalStatus
          data.approvalStatus = '1'
          storageAudit(data).then(res => {
            this.getDetailData()
            this.getConfig()
            this.getWarehouseList()
            this.$modal.msgSuccess("审核成功")
            this.open = false
          })
        }
      })
    },
    // 拒绝
    disagreeApprove(){
      queryWarehouse().then(res => {
        if(res.code == 200){
          // 转换仓库
          let arr = res.data.find(e => {
            return e.name == this.detailData.warehouse
          })
          if(arr != undefined){
            this.detailData.warehouse = arr.warehouseId
          }
          this.detailData.detailList.forEach(i => {
            let dataList = res.data.find(item => {
              return item.name == i.warehouse
            })
            if(dataList != undefined){
              i.warehouse = dataList.warehouseId
            }
          })
          let data = this.detailData
          data.oldApprovalStatus = this.detailData.approvalStatus
          data.approvalStatus = '3'
          storageAudit(data).then(res => {
            this.getDetailData()
            this.getConfig()
            this.getWarehouseList()
            this.$modal.msgSuccess("操作成功")
            this.open = false
          })
        }
      })
    },
    // 确认
    confirm(){
      // console.log(this.detailData)
      let enableBatches = this.detailData.detailList.every(val => {
        if(val.enableBatches == '0'){
          return !!val.batch
        }else{
          return !val.batch
        }
      })
      let index = this.detailData.detailList.findIndex(val => val.endNum !== '')
      let warehouse = this.detailData.detailList.every(val => !!val.warehouse)
      if(warehouse == true){
        if(enableBatches == true){
          if(this.detailData.detailList.length > 1){
            if(index > -1){
              let num = this.detailData.detailList.every(val => {
                if(val.endNum < val.pendingStorage){
                  return false
                }else{
                  return true
                }
              })
              this.detailData.detailList.forEach(item => {
                if(item.endNum == ''){
                  item.endNum = 0
                }
              })
              let arr = this.detailData
              arr.noSubmission = '1' // 免提交
              if(this.detailData.orderType == 1){
                arr.splitOrNo = '' // 不拆单
                this.getStorageConfirm(arr)
              }else{
                if(num == true){
                  arr.status = '1'
                  arr.approvalStatus = '1' // 已通过
                  this.getStorageConfirm(arr)
                }else{
                  detailConfig('splitOrder', 'purInbound').then(res => {
                    let ifAllow = res.data[0].ifAllow
                    if(ifAllow == '0'){
                      this.$modal.confirm('是否拆单？', '拆单').then(() => {
                        arr.status = '1'
                        arr.splitOrNo = '0' // 拆单
                        arr.approvalStatus = '1' // 已通过
                        this.getStorageConfirm(arr)
                      }).catch(action => {
                        if(action == 'cancel'){
                          arr.status = '1'
                          arr.splitOrNo = '1' // 不拆单
                          arr.approvalStatus = '1' // 已通过
                          this.getStorageConfirm(arr)
                        }
                      })
                    }else{
                      this.$modal.msgError('不允许拆单，请编辑实际入库！')
                    }
                  })
                }
              }
            }else{
              this.$modal.msgError('实际入库不能为空！')
            }
          }else{
            // 单条数据
            let endNum = this.detailData.detailList.every(val => !!val.endNum)
            if(endNum == true){
              this.detailData.detailList.forEach(item => {
                let arr = this.detailData
                arr.noSubmission = '1' // 免提交
                if(this.detailData.orderType == 1){
                  arr.splitOrNo = '' // 不拆单
                  this.getStorageConfirm(arr)
                }else{
                  if(item.endNum >= item.pendingStorage){
                    arr.status = '1'
                    arr.approvalStatus = '1' // 已通过
                    this.getStorageConfirm(arr)
                  }else if(item.endNum < item.pendingStorage){
                    // 是否允许拆单
                    detailConfig('splitOrder', 'purInbound').then(res => {
                      let ifAllow = res.data[0].ifAllow
                      if(ifAllow == '0'){
                        this.$modal.confirm('是否拆单？', '拆单').then(() => {
                          arr.status = '1'
                          arr.splitOrNo = '0' // 拆单
                          arr.approvalStatus = '1' // 已通过
                          this.getStorageConfirm(arr)
                        }).catch(action => {
                          if(action == 'cancel'){
                            arr.status = '1'
                            arr.splitOrNo = '1' // 不拆单
                            arr.approvalStatus = '1' // 已通过
                            this.getStorageConfirm(arr)
                          }
                        })
                      }else{
                        this.$modal.msgError('不允许拆单，请编辑实际入库！')
                      }
                    })
                  }
                }
              })
            }else{
              this.$modal.msgError('实际入库不能为空！')
            }
          }
        }else{
          this.$modal.msgError('批次号不能为空！')
        }
      }else{
        this.$modal.msgError('仓库不能为空！')
      }
    },
    // 确认/提交
    getStorageConfirm(e){
      this.getDetailData()
      this.getConfig()
      storageConfirm(e).then(res => {
        this.$modal.msgSuccess("操作成功")
        this.$tab.closeOpenPage({ path: "/purchase/purchaseStorage/list" })
      })
    },
    // 打印
    btnPrint(){
      this.$print(this.$refs.print)
    }
  }
}
</script>

<style scoped>
::v-deep .el-input .el-input-group__append{
  padding: 0 10px;
}
.more{
  font-size: 30px;
  cursor: pointer;
}
::v-deep .el-dialog__header{
  display: none;
}
.icon-more-position{
  font-size: 22px;
  margin: 0 5px;
  cursor: pointer;
}
</style>
